﻿' **  Project Home: http://code.google.com/p/yahoo-finance-managed/
' **  
' ******************************************************************************
' **  
' **  Copyright 2011 Marius Häusler
' **  
' **  Licensed under the Apache License, Version 2.0 (the "License");
' **  you may not use this file except in compliance with the License.
' **  You may obtain a copy of the License at
' **  
' **    http://www.apache.org/licenses/LICENSE-2.0
' **  
' **  Unless required by applicable law or agreed to in writing, software
' **  distributed under the License is distributed on an "AS IS" BASIS,
' **  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' **  See the License for the specific language governing permissions and
' **  limitations under the License.
' ** 
' ******************************************************************************


Namespace YahooManaged.Finance.Indicators

    ''' <summary>
    ''' Moving Average Indicator
    ''' </summary>
    ''' <remarks></remarks>
    Public Class MA
        Implements ISingleValueIndicator

        Public Overridable ReadOnly Property Name() As String Implements IIndicator.Name
            Get
                Return "Moving Average"
            End Get
        End Property

        Public Overridable ReadOnly Property IsRealative As Boolean Implements IIndicator.IsRealative
            Get
                Return False
            End Get
        End Property

        Public ReadOnly Property ScaleMaximum() As Double Implements IIndicator.ScaleMaximum
            Get
                Return Double.PositiveInfinity
            End Get
        End Property

        Public ReadOnly Property ScaleMinimum() As Double Implements IIndicator.ScaleMinimum
            Get
                Return Double.NegativeInfinity
            End Get
        End Property


        Public Property Period() As Integer = 12 Implements IIndicator.Period


        ''' <summary>
        ''' Calculate values of Moving Average for historic quote values.
        ''' </summary>
        ''' <param name="values">An unsorted IEnumerable of HistQuoteData.</param>
        ''' <returns>The sorted dictionaries. 1) MA values; 2) Quote values.</returns>
        ''' <remarks></remarks>
        Public Overridable Function Calculate(ByVal values As IEnumerable(Of KeyValuePair(Of Date, Double))) As Dictionary(Of Date, Double)() Implements ISingleValueIndicator.Calculate
            Dim maResult As New Dictionary(Of Date, Double)

            Dim quoteValues As New List(Of KeyValuePair(Of Date, Double))(values)
            quoteValues.Sort(New QuotesSorter)

            Dim valDict As New Dictionary(Of Date, Double)
            For Each item As KeyValuePair(Of Date, Double) In quoteValues
                valDict.Add(item.Key, item.Value)
            Next

            If quoteValues.Count > 0 Then
                Dim ave As Double = 0
                For i As Integer = 0 To quoteValues.Count - 1
                    ave = 0
                    If i + 1 - Me.Period >= 0 Then
                        For n As Integer = i + 1 - Me.Period To i
                            ave += quoteValues(n).Value
                        Next
                        ave = ave / Me.Period
                    Else
                        For n As Integer = 0 To i
                            ave += quoteValues(n).Value
                        Next
                        ave = ave / (i + 1)
                    End If
                    maResult.Add(quoteValues(i).Key, ave)
                Next
            End If

            Return New Dictionary(Of Date, Double)() {maResult, valDict}
        End Function

        Public Overrides Function ToString() As String
            Return Me.Name & " " & Me.Period
        End Function

    End Class




End Namespace
